﻿using Hidistro.Core;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_SpreadPage_Product : WebControl
    {
        Database database = DatabaseFactory.CreateDatabase();
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }
        public string RendHtml()
        {
            StringBuilder sb = new StringBuilder();
            if (!string.IsNullOrEmpty(this.ProductSKU))
            {
                string[] list = this.ProductSKU.Split(',');
                for (int i = 0; i < list.Length; i++)
                {
                    PNandSP t = this.GetInfo(list[i]);
                    sb.Append("<div class=\"thn\">");
                    sb.AppendFormat("<input class=\"dxk\" type=\"radio\" value=\"{0}\" name=\"product\" price=\"{1}\">", list[i], Globals.FormatMoney(t.SalePrice));
                    sb.AppendFormat("<label>{0}&nbsp;<span style=\"color: #F00\">售 价：￥{1}</span></label>", t.ProductName+"【"+t.SkuName+"】", Globals.FormatMoney(t.SalePrice));
                    sb.Append("</div>");
                }
            }
            return sb.ToString();
        }
        private PNandSP GetInfo(string SkuId)
        {
            DbCommand cmd = database.GetSqlStringCommand(string.Format("select t2.ProductName,t1.SalePrice from Hishop_SKUs t1 left join Hishop_Products t2 on t2.ProductId = t1.ProductId where t1.SkuId = '{0}'", SkuId));
            PNandSP t = new PNandSP();
            using (IDataReader reader = database.ExecuteReader(cmd))
            {
                if (reader.Read())
                {
                    t.ProductName = reader["ProductName"].ToString();
                    t.SalePrice = (decimal)reader["SalePrice"];
                }
            }
            string[] arr =SkuId.Split('_');
            if (!string.IsNullOrEmpty(arr[1]))
            {
                DbCommand cmd2 = database.GetSqlStringCommand(string.Format(" select ValueStr from Hishop_AttributeValues  where  ValueId='{0}'", arr[1]));
                using (IDataReader reader = database.ExecuteReader(cmd2))
                {
                    if (reader.Read())
                    {
                        t.SkuName = reader["ValueStr"].ToString();
                    }
                }
            }
            return t;
        }
        public string ProductID { get; set; }
        public string ProductCode { get; set; }
        public string ProductSKU { get; set; }
    }
    struct PNandSP
    {
        public string ProductName;
        public decimal SalePrice;
        public string SkuName;
    }
}
